from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime

# ==================== 告警数据模型 ====================

class AlarmBase(BaseModel):
    """
    告警数据模型基类
    包含所有告警字段
    """
    global_alarm_id: str          # 告警全局标识
    alarm_status: str             # 告警状态
    alarm_title: str              # 告警标题
    alarm_type: str               # 告警类型
    alarm_level: str              # 标准告警级别 (critical/major/minor/warning)
    occurrence_time: datetime     # 告警发生时间
    clear_time: Optional[datetime] = None  # 告警清除时间
    region: str                   # 大区
    datacenter: str               # 机房
    device_name: str              # 设备名称
    device_type: str              # 设备类型
    device_vendor: str            # 设备厂商（虚层）
    dispatch_status: str          # 派单状态
    resource_pool: str            # 资源池
    alarm_content: str            # 告警正文

class AlarmCreate(AlarmBase):
    """创建告警的请求模型"""
    pass

class AlarmUpdate(BaseModel):
    """更新告警的请求模型（所有字段可选）"""
    alarm_status: Optional[str] = None
    alarm_title: Optional[str] = None
    alarm_type: Optional[str] = None
    alarm_level: Optional[str] = None
    occurrence_time: Optional[datetime] = None
    clear_time: Optional[datetime] = None
    region: Optional[str] = None
    datacenter: Optional[str] = None
    device_name: Optional[str] = None
    device_type: Optional[str] = None
    device_vendor: Optional[str] = None
    dispatch_status: Optional[str] = None
    resource_pool: Optional[str] = None
    alarm_content: Optional[str] = None

class Alarm(AlarmBase):
    """告警响应模型（包含数据库ID）"""
    id: int
    
    class Config:
        orm_mode = True

class AlarmQuery(BaseModel):
    """告警查询条件模型"""
    global_alarm_id: Optional[str] = None
    alarm_status: Optional[str] = None
    alarm_title: Optional[str] = None
    alarm_type: Optional[str] = None
    alarm_level: Optional[str] = None
    region: Optional[str] = None
    datacenter: Optional[str] = None
    device_name: Optional[str] = None
    device_type: Optional[str] = None
    device_vendor: Optional[str] = None
    dispatch_status: Optional[str] = None
    resource_pool: Optional[str] = None
    start_time: Optional[datetime] = None
    end_time: Optional[datetime] = None

class AnalysisResult(BaseModel):
    """告警分析结果模型"""
    total_alarms: int
    alarms_by_level: dict
    alarms_by_type: dict
    alarms_by_status: dict
    alarms_by_region: dict
    alarms_by_datacenter: dict
    alarms_by_dispatch_status: dict
    top_alarms: List[Alarm]